package zhu.areas.match.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import zhu.areas.hatchnav.data.Article;
import zhu.areas.match.data.CompApp;
import zhu.areas.match.data.MatchType;
import zhu.common.dao.BaseDAOImpl;
import zhu.common.data.Pager;
import zhu.common.utils.IPagerManager;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class CompAppDAOImpl extends BaseDAOImpl implements ICompAppDAO {
	
	private IPagerManager pagerManager;
	
	public void saveCompApp(CompApp compApp) {
		super.saveOrUpdate(compApp);
		//Session session=getSession();
	}

	public CompApp findCompAppById(int id) {
		return (CompApp) super.findById(CompApp.class, id);
	}

	public List<CompApp> findAllCompApp() {
		return (List<CompApp>)super.find("from CompApp");
	}

	public List<CompApp> findCompAppByStatue(int statueId) {
		return (List<CompApp>)super.find("from CompApp where statue.statueID="+statueId);
	}

	public void updateCompApp(CompApp compApp) {
		super.saveOrUpdate(compApp);
		
	}
	
	public int pubAllPassedComp(){
		return (Integer)getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update CompApp set statue.statueID=5 where  statue.statueID=3";
						Query query = session.createQuery(hql);
						return query.executeUpdate();
					}
				});
	}
	
	public Pager findCompListByStatue(Integer statue, Integer pageSize,
			String currentPage, String pageMethod) {
		String hsql = " from CompApp where statue.statueID = "+statue+" order by pubTime desc";
		return this.pagerManager.getPager(hsql, pageSize, currentPage,
				pageMethod);
	}


	
	
	public IPagerManager getPagerManager() {
		return pagerManager;
	}

	public void setPagerManager(IPagerManager pagerManager) {
		this.pagerManager = pagerManager;
	}

	public List<CompApp> findByExample(CompApp compApp) {
		return (List<CompApp>)getHibernateTemplate().findByExample(compApp);
		
	}

	public List findByProperty(String name, Object value) {
		try {
		return getHibernateTemplate().find(
				"from  CompApp  where " + name + "= '" + value + "' ");
		} catch (RuntimeException re) {
			throw re;
		}
}

	public List<CompApp> findByMap(final Map params) {
		return (List<CompApp>)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hqlParams = ""; // 迭代获取参数并封装为查询所用的格式
				Iterator it = params.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry entry = (Map.Entry) it.next();
					Object key = entry.getKey();
					Object value = entry.getValue();
					if (it.hasNext()) {
						hqlParams = hqlParams + " " + key + " = '" + value
								+ "' and  ";
					} else {
						hqlParams += key + " = '" + value + "' ";
					}
				}
				String hql = "from CompApp  where " + hqlParams;
				// Query query = session.createQuery(hql);
				// return query.list();
				return session.createQuery(hql).list();
			}
		});
	}

	public CompApp findByPropertyUnique(final String name, final Object value) {

		return  (CompApp)getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "from  CompApp  where  " + name + " = '"
								+ value + "'";
						Query query = session.createQuery(hql);
						query.setMaxResults(1);
						return query.uniqueResult();
					}
				});

	}
	
	public List<CompApp> findByField(String hql) {		
		return null;
	}

	

	
}
